﻿using Report_IService.Web;
using Report_Service.Web;
using Report_Unity.Basic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Web;
using System.Web.Http;

namespace ReportWebApi.Controllers.Api.WeiXin
{

    [RoutePrefix("api/Employee")]
    public class EmployeeController : BaseApiController
    {
        private IT_EmployeeService T_EmployeeService = new T_EmployeeService();

        [HttpPost,Route("AddEmployee")]
        public ResultInfo AddEmployee()
        {
            var req = HttpContext.Current.Request;
            string hid = req.Form["hid"];
            string cid = req.Form["cid"];
            string name = req.Form["name"];
            string idcard = req.Form["idcard"];
            string phone = req.Form["phone"];
            string userid = req.Form["userid"];

            ResultInfo res = T_EmployeeService.AddEmployee(hid,cid,name,idcard,phone,userid);
            return res;
        }

        [HttpPost, Route("EditEmployee")]
        public ResultInfo EditEmployee()
        {
            var req = HttpContext.Current.Request;
            string id = req.Form["id"];
            string hid = req.Form["hid"];
            string cid = req.Form["cid"];
            string name = req.Form["name"];
            string idcard = req.Form["idcard"];
            string phone = req.Form["phone"];
            string userid = req.Form["userid"];

            ResultInfo res = T_EmployeeService.EditEmployee(id,hid,cid,name,idcard,phone,userid);
            return res;
        }

        [HttpPost, Route("DelEmployee")]
        public ResultInfo DelEmployee()
        {
            var req = HttpContext.Current.Request;
            string id = req.Form["id"];
            string hid = req.Form["hid"];
            string cid = req.Form["cid"];

            ResultInfo res = T_EmployeeService.DelEmployee(id,hid,cid);
            return res;
        }

        [HttpGet, Route("GetListEmployee")]
        public ResultInfo GetListEmployee(string page, string size, string name, string idcard, string phone, string sex)
        {
            ResultInfo res = T_EmployeeService.GetListEmployee(page,size,name,idcard,phone,sex);
            return res;
        }

        [HttpPost, Route("ImportExcel")]
        public ResultInfo ImportExcel()
        {
            var req = HttpContext.Current.Request;
            string hid = req.Form["hid"];
            string cid = req.Form["cid"];
            string userid = req.Form["userid"];
            var file = req.Files[0];

            ResultInfo res = T_EmployeeService.ImportExcel(file,hid,cid,userid);
            return res;
        }

        [HttpPost, Route("ChanageLeaderType")]
        public ResultInfo ChanageLeaderType()
        {
            var req = HttpContext.Current.Request;
            string id = req.Form["id"];
            string hid = req.Form["hid"];
            string cid = req.Form["cid"];

            ResultInfo res = T_EmployeeService.ChanageLeaderType(id,hid,cid);
            return res;
        }

        [HttpGet, Route("GetExcelTemplate")]
        public HttpResponseMessage GetExcelTemplate()
        {
            byte[] bys =T_EmployeeService.GetExcelTemplate();
            HttpResponseMessage response = new HttpResponseMessage();
            response.Content = new ByteArrayContent(bys);
            response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
            response.StatusCode = HttpStatusCode.OK;
            response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
            {

                FileName = HttpUtility.UrlEncode($"{DateTime.Now.ToFileTime()}.xls", Encoding.UTF8),
            };

            return response;
        }
    }
}
